/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.product.controller;

import cn.tedu.mall.common.pojo.product.dto.BrandAddNewDTO;
import cn.tedu.mall.common.pojo.product.dto.BrandUpdateDTO;
import cn.tedu.mall.common.pojo.product.vo.BrandStandardVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.product.constant.WebConst;
import cn.tedu.mall.product.service.IBrandService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>品牌控制器</p>
 *
 *  2022-09-09
 */
@Api(tags = "2. 品牌管理模块")
@RestController
@RequestMapping("/pms/brands")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    /**
     * 增加品牌
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "增加品牌", notes = "需要商品后台【写】权限：/pms/product/update")
    @PostMapping("/addnew")
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddnewDTO) {
        brandService.addNew(brandAddnewDTO);
        return JsonResult.ok();
    }

    /**
     * 删除品牌
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "删除品牌", notes = "查询关联，删除无关联数据，需要商品后台【写】权限：/pms/product/update")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "品牌id", paramType = "path", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        brandService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 更新品牌
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "更新品牌", notes = "需要商品后台【写】权限：/pms/product/update")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "品牌id", paramType = "path", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @Valid BrandUpdateDTO brandUpdateDTODTO) {
        brandService.updateFullInfoById(id, brandUpdateDTODTO);
        return JsonResult.ok();
    }

    /**
     * 查询品牌详情
     */
    @ApiOperationSupport(order = 40)
    @ApiOperation(value = "查询品牌详情", notes = "需要商品后台【读】权限：/pms/product/read")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "品牌id", paramType = "path", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<BrandStandardVO> getById(@PathVariable Long id) {
        return JsonResult.ok(brandService.getById(id));
    }

    /**
     * 查询品牌列表
     */
    @ApiOperationSupport(order = 41)
    @ApiOperation(value = "查询品牌列表", notes = "需要商品后台【读】权限：/pms/product/read")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @GetMapping("")
    public JsonResult<JsonPage<BrandStandardVO>> list(
        @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE) Integer page,
        @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        return JsonResult.ok(brandService.list(page, pageSize));
    }

    /**
     * 根据类别查询品牌列表
     */
    @ApiOperationSupport(order = 42)
    @ApiOperation(value = "根据类别查询品牌列表", notes = "需要商品后台【读】权限：/pms/product/read")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "categoryId", value = "类别id", required = true, dataType = "long"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @GetMapping("/by-category")
    public JsonResult<List<BrandStandardVO>> listByCategoryId(
        @RequestParam Long categoryId,
        @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE) Integer page,
        @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<BrandStandardVO> brands = brandService.listByCategoryId(categoryId, page, pageSize);
        List<BrandStandardVO> brandsList = brands.getList();
        return JsonResult.ok(brandsList);
    }

    //重建缓存用于刷新页面
    @PostMapping("/rebuild-cache")
    @ApiOperationSupport(order = 99)
    public JsonResult<Void> rebuildCache() {
        brandService.rebuildCache();
        return JsonResult.ok();
    }
}
